import React, {useRef} from 'react';
import store from "../store";
import {addGoods} from "../store/slice/goods";
import {useNavigate} from "react-router-dom";

function AddGoods(props) {
    const navigate = useNavigate()
    const goodsNameRef = useRef()
    const goodsPriceRef = useRef()
    const subMitGoods = function (e){
        e.preventDefault()
        const goodsNameVal = goodsNameRef.current.value.trim()
        const goodsPriceVal = goodsPriceRef.current.value.trim()/1
        if(!goodsPriceVal||!goodsNameVal)return alert('请输入商品~')
        store.dispatch(addGoods({goodsName:goodsNameVal,goodsPrice:goodsPriceVal}))
        navigate('/goodsList')
    }
    return (
        <div>
            <form onSubmit={subMitGoods}>
                <p>商品名称：<input ref={goodsNameRef} type="text"/></p>
                <p>商品价格：<input ref={goodsPriceRef} type="number"/></p>
                <button>提交</button>
            </form>
        </div>
    );
}

export default AddGoods;